/*
 * Example: 
 *
 * Date: 
 *
 * Reference:
 * https://www10.informatik.uni-erlangen.de/en/Teaching/Courses/SS2013/CPP/schmidt_1.pdf 
 * http://sourcemaking.com/design_patterns/singleton/cpp/2
 * 
 * Note:  
 * 1. The getInstance() function has to be static function.
 * 2. The constructor has to be private function.
 * 3. The instance pointer has to be static variable.
 * 4. the instance pointer has to be initialized to be NULL in the global scope.
 *
 */ 

#include <iostream>

using namespace std;

class MySingleton
{
  public:
    static MySingleton *getInstance()
    {
      if(instance == NULL)
      {
        instance = new MySingleton();
      }
      return instance;
    }

    int getValue()
    {
      return this->value;
    }

    void setValue(int v)
    {
      this->value = v;
    }

  private:
    MySingleton(): value(0)
    {

    }

    //virtual ~MySingleton();
    static MySingleton *instance;
    int value;
};

MySingleton *MySingleton::instance = NULL;

int main()
{

  MySingleton *mySingleton = MySingleton::getInstance();
  cout << "mySingleton->getValue() = " << mySingleton->getValue() << endl;
  mySingleton->setValue(10);
  cout << "mySingleton->getValue() = " << mySingleton->getValue() << endl;

  MySingleton *mySingleton_2 = MySingleton::getInstance();
  cout << "mySingleton-.getValue() = " << mySingleton->getValue() << endl;

  return 0;
}
